package com.hyxt.DTO;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hyxt.api.T809ServerService;
import com.hyxt.api.TermianalLinkServer;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author shenp
 * @version v1.0
 * @Description 启动监听终端-服务端
 * @Date: Create in 15:48 2017/11/30
 * @Modifide By:
 **/
@Component
public class StartTermianalServer {
    @Reference(version = "1.0.0")
    TermianalLinkServer termianalLinkServer;
    public void start(){
        Properties prop = new Properties();
        List portlist = new ArrayList();
        try{
            //获取端口
             InputStream in = new BufferedInputStream(new FileInputStream("src/main/resources/termianal.properties"));
             prop.load(in);
             String port = prop.getProperty("port");

            if (port.contains("-")) {
                // 截串
                String[] p = port.split("-");
                // 判断长度是否大于2
                if (p.length == 2) {
                    // 转类型
                    int startport = Integer.parseInt(p[0]);
                    // 转类型
                    int endport = Integer.parseInt(p[1]);
                    // 判断第二个端口是否大于第一个
                    if (endport > startport) {
                        // 取出范围内所有的端口号
                        for (int i = startport; i <= endport; i++) {
                            // 放到集合中
                            portlist.add(i);
                        }
                    }
                }
            } else {
                // 固定端口
                portlist.add(new Integer(port).intValue());
            }
            termianalLinkServer.startServer(portlist);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
